const result = require("./handle");

//校验位undefind
function unDefined(arrAy) {
  console.log(arrAy);
  arrAy.forEach((item) => {
    if (item === undefined) {
      throw new result("参数填写错误，请检查字段名", 400);
    }
  });
}
//校验为空
function nullValue(arrAy) {
  arrAy.forEach((item) => {
    if (typeof item.value === "string") {
      if (item.value.trim() === "") {
        throw new result(item.tips, 422);
      }
    }
  });
}
const mobilever = /^1[3-9]\d{9}$/;
const passwordver = /^(?=\S*?[a-zA-Z])(?=\S*?\d)\S{6,20}$/;

//小程序端，登录校验
const Login = (mobile, password) => {
  unDefined([mobile, password]);
  nullValue([
    { value: mobile, tips: "请输入手机号码" },
    { value: password, tips: "请输入密码" },
  ]);
  // 校验手机号
  if (!mobilever.test(mobile)) {
    throw new result("手机号格式不正确", 422);
  }
};

//小程序端，发送验证码前校验
const canSendCaptcha = (phoneNumbers) => {
  unDefined([phoneNumbers]);
  nullValue([{ value: phoneNumbers, tips: "请输入手机号码" }]);
  // 校验手机号
  if (!mobilever.test(phoneNumbers)) {
    throw new result("手机号格式不正确", 422);
  }
};
//小程序端，手机号登录校验
const Mobileregistation = (mobile, code, bizId) => {
  unDefined([mobile, code, bizId]);
  nullValue([
    { value: mobile, tips: "请输入手机号码" },
    { value: code, tips: "请输入验证码" },
    { value: bizId, tips: "请输入验证码id" },
  ]);
  // 校验
  if (!mobilever.test(mobile)) {
    throw new result("手机号格式不正确", 422);
  }
};

//小程序端，设置密码
const Uploadpassword = (mobile, code, bizId, password) => {
  unDefined([mobile, code, bizId, password]);
  nullValue([
    { value: mobile, tips: "请输入手机号码" },
    { value: code, tips: "请输入验证码" },
    { value: bizId, tips: "请输入验证码id" },
    { value: password, tips: "请输入密码" },
  ]);
  // 校验手机号
  if (!mobilever.test(mobile)) {
    throw new result("手机号格式不正确", 422);
  }
};
//编辑个人资料
const Modifytheuser = (
  avatarUrl,
  nickname,
  gender,
  birthday,
  city,
  backdrop
) => {
  unDefined([avatarUrl, nickname, gender, birthday, city, backdrop]);
  nullValue([
    { value: avatarUrl, tips: "请上传头像地址" },
    { value: nickname, tips: "请输入昵称" },
    { value: gender, tips: "请选择性别" },
    { value: birthday, tips: "请选择生日" },
    { value: city, tips: "请选择城市" },
    { value: backdrop, tips: "请上传背景" },
  ]);
};

//分析游记关键词
const ArticleKeyword = (text) => {
  unDefined([text]);
};

//发布游记：图片类型
const Atrcliepblish = (title, content, image, address, province, tag) => {
  unDefined([title, content, image, address, province, tag]);
  nullValue([
    { value: title, tips: "请填写标题" },
    { value: content, tips: "请输入游记内容" },
    { value: image, tips: "请上传图片" },
    { value: address, tips: "请选择城市" },
    { value: province, tips: "请选择城市" },
    { value: tag, tips: "请添加标签" },
  ]);
  if (!Array.isArray(image)) {
    throw new result("image字段应为数组类型", 422);
  }
  if (image.length === 0) {
    throw new result("至少上传一张图片", 422);
  }
  if (!Array.isArray(image)) {
    throw new result("tag字段应为数组类型", 422);
  }
  if (image.length === 0) {
    throw new result("请添加标签", 422);
  }
};
//发布游记：视频类型
const AtrcliepblishVideo = (
  title,
  content,
  city,
  address,
  province,
  tag,
  videoPoster,
  videoUrl,
  videoWidth,
  videoHeight
) => {
  unDefined([
    title,
    content,
    city,
    address,
    province,
    tag,
    videoPoster,
    videoUrl,
    videoWidth,
    videoHeight,
  ]);
  nullValue([
    { value: title, tips: "请填写标题" },
    { value: content, tips: "请输入游记内容" },
    { value: address, tips: "请选择城市" },
    { value: province, tips: "请选择城市" },
    { value: tag, tips: "请添加标签" },
    { value: videoPoster, tips: "请上传封面图" },
    { value: videoUrl, tips: "请上传视频链接" },
    { value: videoWidth, tips: "缺少视频宽度" },
    { value: videoHeight, tips: "缺少视频高度" },
  ]);
  // if (!Array.isArray(videoPoster)) {
  //   throw new result("videoPoster字段应为数组类型", 422);
  // }
  if (videoPoster.length === 0) {
    throw new result("至少上传一张图片", 422);
  }
  if (!Array.isArray(tag)) {
    throw new result("tag字段应为数组类型", 422);
  }
  if (tag.length === 0) {
    throw new result("请添加标签", 422);
  }
};

//注册账号：管理员端
const Adminregister = (mobile, password) => {
  unDefined([mobile, password]);
  nullValue([
    { value: mobile, tips: "请输入手机号" },
    { value: password, tips: "请输入密码" },
  ]);
  // 校验手机号
  if (!mobilever.test(mobile)) {
    throw new result("手机号格式不正确", 422);
  }
  if (!passwordver.test(password)) {
    throw new result("密码为6-20位数字和字母结合", 422);
  }
};

//更新管理员头像，昵称
const ModifyuserInfo = (_id, nickname, avatarUrl) => {
  unDefined([_id, nickname, avatarUrl]);
  nullValue([
    { value: _id, tips: "缺少_id" },
    { value: avatarUrl, tips: "请上传头像" },
    { value: nickname, tips: "请输入昵称" },
  ]);
};

//上传每日推荐
const Dailyrecom = (imageUrl, title, address, color) => {
  unDefined([imageUrl, title, address, color]);
  nullValue([
    { value: imageUrl, tips: "请上传封面图" },
    { value: title, tips: "请填写标题" },
    { value: address, tips: "请输请填写地址" },
    { value: color, tips: "请选择颜色" },
  ]);
};

//搜索省市数据
const Serchtravel = (keywords) => {
  unDefined([keywords]);
  nullValue([{ value: keywords, tips: "请填写地址" }]);
};

//获取每日推荐
const Resreturn = (page) => {
  unDefined([page]);
  nullValue([{ value: page, tips: "请填写地址" }]);
};

//删除每日推荐
const CompanionDatails = (_id) => {
  unDefined([_id]);
  nullValue([{ value: _id, tips: "缺少_id" }]);
};
//提交4个推荐
const UploadfourTravel = (imageUrl, travel_id) => {
  unDefined([imageUrl, travel_id]);
  nullValue([
    { value: imageUrl, tips: "请上传图片" },
    { value: travel_id, tips: "缺少关联_id" },
  ]);
};

//修改更新四个游记
const ModifyfourTravel = (_id, imageUrl, travel_id) => {
  unDefined([_id, imageUrl, travel_id]);
  nullValue([
    { value: imageUrl, tips: "请上传图片" },
    { value: travel_id, tips: "缺少关联_id" },
    { value: _id, tips: "缺少_id" },
  ]);
};

//评论游记
const Comment = (article_id, comment_content) => {
  unDefined([article_id, comment_content]);
  nullValue([
    { value: article_id, tips: "缺少_id" },
    { value: comment_content, tips: "请填写评论" },
  ]);
};

//获取评论数据
const Commentget = (article_id, page) => {
  unDefined([article_id, page]);
  nullValue([
    { value: article_id, tips: "缺少_id" },
    { value: page, tips: "缺少page" },
  ]);
};

//关注接口
const Concern = (im_concerned_uid) => {
  unDefined([im_concerned_uid]);
  nullValue([{ value: im_concerned_uid, tips: "缺少作者uid" }]);
};

//获取瀑布流
const Classifytravels = (keywords, page) => {
  unDefined([keywords, page]);
  nullValue([
    { value: keywords, tips: "缺少分类查询词" },
    { value: page, tips: "缺少page" },
  ]);
};

//根据定位获取当地游记
const LocalTravel = (page, address) => {
  unDefined([page, address]);
  nullValue([
    { value: page, tips: "缺少page" },
    { value: address, tips: "缺少地址" },
  ]);
};

//当地玩法：按地址和游记分类查询游记
const AddressQueryTravels = (address, keywords, page) => {
  unDefined([address, keywords, page]);
  nullValue([
    { value: address, tips: "缺少地址" },
    { value: keywords, tips: "缺少查询词" },
    { value: page, tips: "缺少page" },
  ]);
};
//用户根据分类选择地址
const ChooseAddress = (type) => {
  unDefined([type]);
  nullValue([{ value: type, tips: "缺少type" }]);
};

//发布结伴
const PublishCompanion = (
  description,
  image,
  city,
  full_address,
  companion_time,
  number_of_people
) => {
  unDefined([
    description,
    image,
    city,
    full_address,
    companion_time,
    number_of_people,
  ]);
  nullValue([
    { value: description, tips: "请填写描述" },
    { value: image, tips: "请上传封面图" },
    { value: city, tips: "请选择城市" },
    { value: full_address, tips: "请填写目的地详细地址" },
    { value: companion_time, tips: "请填写结伴时间" },
    { value: number_of_people, tips: "请填写人数" },
  ]);
};

//报名表
const SignUpPartner = (singup_id, contact_inform, gender, introduce) => {
  unDefined([singup_id, contact_inform, gender, introduce]);
  nullValue([
    { value: singup_id, tips: "缺少_id" },
    { value: contact_inform, tips: "请填写联系方式" },
    { value: gender, tips: "请选择性别" },
    { value: introduce, tips: "请填写自我介绍" },
  ]);
};

//首页筛选活动
const CompanionQuery = (city, keywords, page) => {
  unDefined([city, keywords, page]);
  nullValue([
    { value: city, tips: "缺少城市" },
    { value: keywords, tips: "缺少查询词" },
    { value: page, tips: "缺少page" },
  ]);
};

//获取结伴详情
const CompanionDetails = (id) => {
  unDefined([id]);
  nullValue([{ value: id, tips: "缺少_id" }]);
};

//管理成员
const ManagingMember = (id, page) => {
  unDefined([id, page]);
  nullValue([
    { value: id, tips: "缺少_id" },
    { value: page, tips: "缺少page" },
  ]);
};

module.exports = {
  Login, //登录
  canSendCaptcha, //发送验证码
  Mobileregistation, //注册账号
  Uploadpassword, //修改密码
  Modifytheuser, //编辑个人资料
  ArticleKeyword, //分析游记关键词
  Atrcliepblish, //发布游记：图片类型
  AtrcliepblishVideo, //发布游记：视频类型
  Adminregister, //注册账号：管理员端
  ModifyuserInfo, //更新管理员头像，昵称
  Dailyrecom, //上传每日推荐
  Serchtravel, //搜索省市数据
  Resreturn, //获取每日推荐
  CompanionDatails, //删除每日推荐
  UploadfourTravel, //提交4个推荐
  ModifyfourTravel, //修改更新4个推荐
  Comment, //评论
  Commentget,
  Concern,
  Classifytravels,
  LocalTravel,
  AddressQueryTravels,
  ChooseAddress,
  PublishCompanion,
  SignUpPartner,
  CompanionQuery,
  CompanionDetails,
  ManagingMember,
};
